import { VuexModule, Module, Action, getModule } from 'vuex-module-decorators'
import store from '@/store'
import { getRooms, insertRoom, deleteRoom, updateRoom, batchSaveRooms } from '@/api/rooms'
import { IRoomModel } from '@/api/types'

export interface IRoomState {

}

@Module({ dynamic: true, store, name: 'room' })
class Rooms extends VuexModule implements IRoomState {

    @Action
    public async getRooms() {
        return new Promise((r: any, j: any) => {
            getRooms({}).then((value: any) => {
                console.error("Rooms:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async insertRoom(obj: any) {
        return new Promise((r: any, j: any) => {
            insertRoom(obj).then((value: any) => {
                console.error("Rooms:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async updateRoom(obj: any) {
        return new Promise((r: any, j: any) => {
            updateRoom(obj).then((value: any) => {
                console.error("Rooms:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async deleteRooms(roomId: string) {
        return new Promise((r: any, j: any) => {
            deleteRoom({ roomId: roomId }).then((value: any) => {
                console.error("Rooms:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

    @Action
    public async batchSaveRooms(rooms: Array<IRoomModel>) {
        return new Promise((r: any, j: any) => {
            batchSaveRooms({ rooms: rooms }).then((value: any) => {
                console.error("Rooms:", value)
                r(value.data)
            }).catch(ex => {
                console.error(ex);
                j([])
            })
        })
    }

}

export const RoomsModule = getModule(Rooms)
